<template>
<!-- 这里把scroll标签引进来下面可以通过ref直接调用scroll里的方法 -->
  <div class="recommend">
  	<scroll ref="scroll" class="recommend-content" :data="discList">
      <div>
    	  <div v-if="recommends.length" class="recommend-wrapper">
    	  	<slider>
            <div v-for="item in recommends">
              <a :href="item.linkUrl">
                <img :src="item.picUrl" />
              </a>
            </div>  
          </slider>
    	  </div>
    	  <div class="recommend-list">
    	  	<h1 class="list-title">热门歌单推荐</h1>
    	  	<ul>
    	  		<li v-for="item in discList" :key="item.dissid" class="item">
              <div class="icon">
              <!-- 将:src替换成v-lazy可以实现懒加载懒加载只实现首页请求当滚动时再继续进行请求 -->
              <!-- 这个neesclick是由fastclick来监听的当监听到这个class时就不会阻止点击因为fastclick和better-scroll的点击事件是有相斥 -->
                <img class="needsclick" width="60px" height="60px" v-lazy="item.imgurl" @load="loadImage">
              </div>
              <div class="text">
                <div class="name" v-html="item.creator.name"></div> 
                <div class="desc" v-html="item.dissname"></div> 
              </div> 
            </li>
    	  	</ul>
    	  </div>
      </div>
      <!-- 数组当用这种if一定后面接length -->
      <div class="loading-container" v-show="!discList.length">
        <loading></loading>
      </div>
  	</scroll>
  </div>
</template>

<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  import Slider from 'base/slider/slider'
  import { getRecommend , getDiscList} from 'api/recommend'
  import { ERR_OK } from 'api/config'
  import Loading from 'base/loading/loading'
  export default {
    created () {
      this._getRecommend()
      this._getDiscList()
    },
    data () {
      return {
        recommends: [],
        discList: []
      }
    },
    methods : {
      //这个方法就是利用jsonp进行取数据取轮播图数据就是这个slider
    	_getRecommend () {
    		getRecommend().then((res) => {
    			if (res.code === ERR_OK) {
    				this.recommends = res.data.slider
    			}
    		})
    	},
      _getDiscList () {
        getDiscList().then((res) => {
          if (res.code === ERR_OK) {
            this.discList = res.data.list
          }
        })
      },
      //这个checked就是一个标记位证明这个函数只进行一次
      loadImage () {
        if (this.checked) {
          this.$refs.scroll.refresh()
          this.checked = false
        }
      }
    },
    components: {
      Slider,
      Scroll,
      Loading
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .recommend-content
      height: 100%
      overflow: hidden
      .slider-wrapper
        position: relative
        width: 100%
        overflow: hidden
      .recommend-list
        .list-title
          height: 65px
          line-height: 65px
          text-align: center
          font-size: $font-size-medium
          color: $color-theme
        .item
          display: flex
          box-sizing: border-box
          align-items: center
          padding: 0 20px 20px 20px
          .icon
            flex: 0 0 60px
            width: 60px
            padding-right: 20px
          .text
            display: flex
            flex-direction: column
            justify-content: center
            flex: 1
            line-height: 20px
            overflow: hidden
            font-size: $font-size-medium
            .name
              margin-bottom: 10px
              color: $color-text
            .desc
              color: $color-text-d
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
